import 'package:flutter/material.dart';
import 'package:flutter_sample/tab/bean/article_bean.dart';
import 'package:flutter_sample/tab/data/article_repo.dart';
import 'package:flutter_sample/tab/ui/home/article/ui_article_item.dart';

class UIArticles extends StatefulWidget {
  final String category;

  const UIArticles({Key? key, required this.category})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _ArticleState();
}

class _ArticleState extends State<UIArticles> with AutomaticKeepAliveClientMixin {
  List<ArticleBean> data = [];

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    ArticleRepo.instance
        .getArticles(widget.category, 1)
        .then((value) => {
              setState(() {
                data = value;
              })
            });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView.separated(
        itemCount: data.length,
        itemBuilder: (BuildContext context, int index) => Padding(
              padding: const EdgeInsets.all(10.0),
              child: UIArticleItem(data[index]),
            ),
        separatorBuilder: (BuildContext context, int index) => const Padding(
              padding: EdgeInsets.only(
                left: 10.0,
                right: 10.0,
              ),
              child: Divider(height: 1.0, color: Colors.grey),
            ));
  }
}
